package com.example.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.example.administrator.videoplay.R;
import com.example.app.manager.MediaPlayerManager;
import com.example.app.manager.c1.Camera1Manager;
import com.example.app.view.c1.PreviewC1Layout;

import java.io.File;

/**
 * 视频录制
 * Created by 郭敏 on 2018/1/31 0031.
 */

public class VideoActivity extends Activity implements View.OnClickListener {

    private PreviewC1Layout previewC1Layout;


    private boolean isRecording = false;//标记是否已经在录制
    private Camera1Manager cameraManager;
    private int w;
    private int h;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setWindow();
        setContentView(R.layout.activity_video);
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        w = displayMetrics.widthPixels;
        h = displayMetrics.heightPixels;
        MLog.e("-----", "-----" + "w:" + w + " h:" + h);

        cameraManager = new Camera1Manager(this);
        initViews();
    }

    private String type;

    private void setWindow() {
        Intent it = getIntent();
        type = it.getStringExtra("arg0");
        //
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        if ("1".equals(type)) {
            // 设置横屏 90度
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        if ("2".equals(type)) {
            // 设置横屏 270度
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
        }
        if ("3".equals(type)) {
            // 设置竖屏显示 0度
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        if ("4".equals(type)) {
            // 设置竖屏显示 180度
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
        }
        //选择支持半透明模式,在有surfaceview的activity中使用。
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
    }

    private Button videoBtn;


    private void initViews() {
        videoBtn = (Button) findViewById(R.id.video_btn);
        videoBtn.setOnClickListener(this);
        findViewById(R.id.video_rest_tv).setOnClickListener(this);
        findViewById(R.id.picture_tv).setOnClickListener(this);
        findViewById(R.id.camera_switchover_tv).setOnClickListener(this);
        findViewById(R.id.camera_light_tv).setOnClickListener(this);
        findViewById(R.id.camera_image_tv).setOnClickListener(this);
        findViewById(R.id.video_pay_tv).setOnClickListener(this);
        previewC1Layout = (PreviewC1Layout) findViewById(R.id.preview_Layout);
        previewC1Layout.onInitView(this, false);
        cameraManager.setTextureView(previewC1Layout.getSurfaceView(), w, h);
        previewC1Layout.setCameraManager(cameraManager);
    }

    /**
     * 初始化相机
     */

    @Override
    protected void onResume() {
        super.onResume();
        // 当屏幕关闭后重新打开, 若SurfaceTexture已经就绪,
        // 此时onSurfaceTextureAvailable不会被回调, 这种情况下
        // 如果SurfaceTexture已经就绪, 则直接打开相机,
        // 否则等待SurfaceTexture已经就绪的回调
        if (previewC1Layout.isAvailable()) {
            cameraManager.onInitCamera();
        } else {
            previewC1Layout.setSurfaceCallback();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        MediaPlayerManager.getInstance().setMediaPlayerStop();
        cameraManager.onCameraClose();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.camera_switchover_tv) {
            //切换摄像头
            cameraManager.setCameraSwitch();
            return;
        }
        if (id == R.id.picture_tv) {
            //拍照
            cameraManager.onCameraTakePicture(getImagePath(), null, null);
            return;
        }
        if (id == R.id.video_rest_tv) {
            //重置预览
            MediaPlayerManager.getInstance().setMediaPlayerStop();
            cameraManager.setRecoverPreview();
            return;
        }
        if (id == R.id.camera_light_tv) {
            //切换灯光
            cameraManager.setCameraLight();
            return;
        }
        if (id == R.id.video_btn) {
            //录制
            if (!isRecording) {
                isRecording = true;
                cameraManager.onVideoStart(getVoidPath());
                videoBtn.setText("停止");
            } else {
                isRecording = false;
                cameraManager.onVideoStop();
                videoBtn.setText("开始");
            }
            return;
        }
        if (id == R.id.video_pay_tv) {
            //播放视频
            cameraManager.onCameraClose();
            MediaPlayerManager.getInstance().setDataSource("", getVoidPath());
            MediaPlayerManager.getInstance().setOnMediaListener(new MediaPlayerListener());
            //
            MediaPlayerManager.getInstance().setSurfaceView(previewC1Layout.getSurfacePlay());
            MediaPlayerManager.getInstance().setMediaWork(0);
            return;
        }
        if (id == R.id.camera_image_tv) {
            //查看图片
            Intent it = new Intent();
            it.setClass(this, ImageActivity.class);
            it.putExtra("arg0", type);
            it.putExtra("arg1", getImagePath());
            startActivity(it);
            return;
        }
    }

    protected String getImagePath() {
        String imagePath = Environment
                .getExternalStorageDirectory().
                        getAbsolutePath() + File.separator +
                "videot" + File.separator + "test.png";
        return imagePath;
    }

    private String getVoidPath() {
        String videoPath = Environment
                .getExternalStorageDirectory().
                        getAbsolutePath() + File.separator
                + "videot";
        File file = new File(videoPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return videoPath + File.separator + "测试4.mp4";
    }


    //播放监听
    class MediaPlayerListener implements MediaPlayerManager.OnMediaPlayerListener {

        @Override
        public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
            //previewLayout.getPreviewView().
            //        setPayCenterCropFill(width, height);
        }


        @Override
        public void onBufferingUpdate(MediaPlayer mp, int percent) {

        }

        @Override
        public void onPayState(MediaPlayer mp, int state, String source) {
            MLog.e("--", "" + state);
            switch (state) {
                case 102:
                    //播放完成重播
                    MediaPlayerManager.getInstance().setMediaWork(2);
                    break;
            }
        }


        @Override
        public void onError(MediaPlayer mp, int what, int extra) {

        }
    }

    @Override
    protected void onDestroy() {
        MediaPlayerManager.getInstance().setMediaPlayerStop();
        super.onDestroy();
    }
}
